import 'package:in_app_purchase/in_app_purchase.dart';

class PurchaseItem {
  final String id;
  final String title;
  final String description;
  final double price;
  final String priceString;
  final String currencyCode;
  final bool isSubscription;
  final int? coins;
  final bool isSpecialOffer;

  PurchaseItem({
    required this.id,
    required this.title,
    required this.description,
    required this.price,
    required this.priceString,
    required this.currencyCode,
    required this.isSubscription,
    this.coins,
    this.isSpecialOffer = false,
  });

  factory PurchaseItem.fromProductDetails(ProductDetails product) {
    // 从商品ID中提取金币数量
    int? coins;
    final match = RegExp(r'(\d+)\s*coins').firstMatch(product.id);
    if (match != null) {
      coins = int.tryParse(match.group(1) ?? '');
    }

    // 如果从ID中没有找到，尝试从title中提取
    if (coins == null) {
      final titleMatch = RegExp(r'(\d+)\s*coins').firstMatch(product.title);
      if (titleMatch != null) {
        coins = int.tryParse(titleMatch.group(1) ?? '');
      }
    }

    // 如果还是没有找到，根据商品ID映射金币数量
    if (coins == null) {
      switch (product.id) {
        case '446200':
          coins = 100;
          break;
        case '446201':
          coins = 500;
          break;
        case '446202':
          coins = 1200;
          break;
        case '446203':
          coins = 2500;
          break;
        case '446204':
          coins = 7000;
          break;
        case '446205':
          coins = 15000;
          break;
        case '446206':
          coins = 500;
          break;
        case '446207':
          coins = 1200;
          break;
        case '446208':
          coins = 2500;
          break;
        case '446209':
          coins = 7000;
          break;
        case '446210':
          coins = 15000;
          break;
      }
    }

    // 判断是否为特价商品
    final bool isSpecialOffer = product.id.contains('special') || 
                               product.title.toLowerCase().contains('special') ||
                               product.description.toLowerCase().contains('special') ||
                               product.id.contains('discount') ||
                               product.title.toLowerCase().contains('discount') ||
                               product.description.toLowerCase().contains('discount') ||
                               ['446206', '446207', '446208', '446209', '446210'].contains(product.id);

    return PurchaseItem(
      id: product.id,
      title: product.title,
      description: product.description,
      price: product.rawPrice,
      priceString: product.price,
      currencyCode: product.currencyCode,
      isSubscription: product.id.contains('subscription'),
      coins: coins,
      isSpecialOffer: isSpecialOffer,
    );
  }

  ProductDetails toProductDetails() {
    return ProductDetails(
      id: id,
      title: title,
      description: description,
      price: priceString,
      rawPrice: price,
      currencyCode: currencyCode,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'price': price,
      'priceString': priceString,
      'currencyCode': currencyCode,
      'isSubscription': isSubscription,
      'coins': coins,
      'isSpecialOffer': isSpecialOffer,
    };
  }
} 